import axios from 'axios'
import Vue from 'vue'
import router from './router'
const http = axios.create({
    baseURL: 'http://localhost:3000/'
})
http.interceptors.request.use(function(config) {
    // Do something before request is sent
    if (localStorage.token) {
        config.headers.Authorization = 'Bearer ' + localStorage.token
    }
    return config;
}, function(error) {
    Vue.prototype.$message.error({
            type: 'error',
            message: error
        })
        // Do something with request error
    return Promise.reject(error);
});


http.interceptors.response.use(function(config) {
    // Do something before request is sent
    if (localStorage.token) {
        config.headers.Authorization = 'Bearer ' + localStorage.token
    }
    return config;
}, function(error) {
    if (error.response.data.message) {
        Vue.prototype.$message.error({
            type: 'error',
            message: error.response.data.message
        })
        console.log('asdf')


    }
    if (error.response.status === 401) {
        router.push('/login')

    }
    // Do something with request error
    return Promise.reject(error);
});

export default http